package com.aier.school.service.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.SchoolTeacherDuty;
import com.aier.school.entity.mapper.SchoolTeacherDutyRowMapper;
import com.aier.school.service.ISchoolDutyService;

@Component("schoolDutyService")
public class SchoolDutyServiceImpl_J implements ISchoolDutyService{

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    
    @Override
    public SchoolTeacherDuty selectbydid(Long did) {
        return (SchoolTeacherDuty) commonDao.listByObject(
                "select * from school_teacher_duty where did=?", new Object[] { did },
                new SchoolTeacherDutyRowMapper());
    }
    
    @Override
    public int insertSchoolTeacherDuty(int sid ,Long tid,Timestamp sdate, Timestamp edate) {
        SchoolTeacherDuty std = new SchoolTeacherDuty();
        std.setSid(sid);
        std.setTid(tid);
        std.setSdate(sdate);
        std.setEdate(edate);
        try {
            commonDao.insert(
                        SchoolTeacherDutyRowMapper.getPreparedStatementCreator(std),
                        new GeneratedKeyHolder());
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteSchoolTeacherDuty(Long did) {
        if (commonDao.update("delete from school_teacher_duty where did=?",
                new Object[] { did }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int updateSchoolTeacherDutyByid(Long did,Timestamp sdate, Timestamp edate) {
        if (commonDao.update(
                "update school_teacher_duty std set std.sdate=?,std.edate=? where std.did=?",
                new Object[] {sdate,edate,did}) > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    @Override
    public Map<String, Object> searchstDutysByCondition(int page,
            int pagesize, int sid,
            List<Long> tids, String sdate, String edate) {
        String sql = " from school_teacher_duty as std where std.sid=? ";
        if (tids.size()>0){
            String temp_tids = "(";
            for(int i=0;i<tids.size();i++){
                if(i==tids.size()-1){
                    temp_tids = temp_tids + tids.get(i) + ")";
                }else{
                    temp_tids = temp_tids + tids.get(i) + ",";
                }
            }
            sql += " and std.tid in " + temp_tids;
        }
        if (sdate!=null&&!sdate.equals("")){
            sql += " and std.sdate >='" + sdate + "'";
        }
        if (edate!=null&&!edate.equals("")){
            sql += " and std.edate <='" + edate + "'";
        }
        
        String sql_first = sql;
        sql += " order by std.edate desc";

        return commonDao
                .queryPage(
                        "select std.*,(select tname from school_teacher where tid=std.tid) as teacherName," +
                        "(select account from user where uid=(select uid from school_teacher where tid=std.tid)) as tAccount," +
                        "(select name from school_grade sc where sc.gid=(select gid from school_teacher where tid=std.tid)) as typeName "
                                + sql, "select count(*) " + sql_first, page,
                        pagesize, new Object[] { sid }, new Object[] { sid },
                        new SchoolTeacherDutyRowMapper(SchoolTeacherDutyRowMapper.AND_TEACHERNAME));        
    }


    
}
